using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Bluetooth;

namespace Zero.Android.RadioUi
{
	[Activity (Label = "Zero.Android.RadioUi", MainLauncher = true)]
	public class MainActivity : Activity
	{
		BluetoothAdapter adapter;
		BluetoothDevice device;
		BluetoothSocket socket;
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			SetupEventHandlers ();

		}

		#region EventHandlers
		private void SetupEventHandlers()
		{
			Button button = FindViewById<Button> (Resource.Id.btnConnect);			
			button.Click += BtnConnectClick;

			Button btnOne = FindViewById<Button> (Resource.Id.btnOne);
			btnOne.Click += HandleClick;

			Button btnTwo = FindViewById<Button> (Resource.Id.btnTwo);
			btnTwo.Click += HandleClick;

			Button btnThree = FindViewById<Button> (Resource.Id.btnThree);
			btnThree.Click += HandleClick;

			Button btnFour = FindViewById<Button> (Resource.Id.btnFour);
			btnFour.Click += HandleClick;

			Button btnFive = FindViewById<Button> (Resource.Id.btnFive);
			btnFive.Click += HandleClick;

			Button btnSix = FindViewById<Button> (Resource.Id.btnSix);
			btnSix.Click += HandleClick;

			Button btnSeven = FindViewById<Button> (Resource.Id.btnSeven);
			btnSeven.Click += HandleClick;

			Button btnEight = FindViewById<Button> (Resource.Id.btnEight);
			btnEight.Click += HandleClick;

			Button btnNine = FindViewById<Button> (Resource.Id.btnNine);
			btnNine.Click += HandleClick;

			Button btnZero = FindViewById<Button> (Resource.Id.btnZero);
			btnZero.Click += HandleClick;

			Button btnDirect = FindViewById<Button> (Resource.Id.btnDirect);
			btnDirect.Click += HandleClick;

			Button btnText = FindViewById<Button> (Resource.Id.btnText);
			btnText.Click += HandleClick;

			Button btnLeft = FindViewById<Button> (Resource.Id.btnLeft);
			btnLeft.Click += HandleClick;

			Button btnUp = FindViewById<Button> (Resource.Id.btnUp);
			btnUp.Click += HandleClick;

			Button btnMenu = FindViewById<Button> (Resource.Id.btnMenu);
			btnMenu.Click += HandleClick;

			Button btnClock = FindViewById<Button> (Resource.Id.btnClock);
			btnClock.Click += HandleClick;

			Button btnScan = FindViewById<Button> (Resource.Id.btnScan);
			btnScan.Click += HandleClick;

			Button btnCatFold = FindViewById<Button> (Resource.Id.btnCatFold);
			btnCatFold.Click += HandleClick;

			Button btnRight = FindViewById<Button> (Resource.Id.btnRight);
			btnRight.Click += HandleClick;

			Button btnDown = FindViewById<Button> (Resource.Id.btnDown);
			btnDown.Click += HandleClick;

			Button btnSound = FindViewById<Button> (Resource.Id.btnSound);
			btnSound.Click += HandleClick;

			Button btnPhone = FindViewById<Button> (Resource.Id.btnPhone);
			btnPhone.Click += HandleClick;

			Button btnRadioPower = FindViewById<Button> (Resource.Id.btnRadioPower);
			btnRadioPower.Click += HandleClick;

			Button btnVolUp = FindViewById<Button> (Resource.Id.btnVolUp);
			btnVolUp.Click += HandleClick;

			Button btnAmFm = FindViewById<Button> (Resource.Id.btnAMFM);
			btnAmFm.Click += HandleClick;

			Button btnCd = FindViewById<Button> (Resource.Id.btnCD);
			btnCd.Click += HandleClick;

			Button btnTuneUp = FindViewById<Button> (Resource.Id.btnTuneUp);
			btnTuneUp.Click += HandleClick;

			Button btnPlayPause = FindViewById<Button> (Resource.Id.btnPlayPauseOk);
			btnPlayPause.Click += HandleClick;

			Button btnVolDown = FindViewById<Button> (Resource.Id.btnVolDown);
			btnVolDown.Click += HandleClick;

			Button btnSirius = FindViewById<Button> (Resource.Id.btnSirius);
			btnSirius.Click += HandleClick;

			Button btnAux = FindViewById<Button> (Resource.Id.btnAux);
			btnAux.Click += HandleClick;

			Button btnTuneDown = FindViewById<Button> (Resource.Id.btnTuneDown);
			btnTuneDown.Click += HandleClick;
		}
            

		protected override void OnPause ()
		{
			if (socket != null) {
				//socket.Close ();
				//Button btn = FindViewById<Button> (Resource.Id.btnConnect);
				//btn.Text = "Disconnect";
			}
			base.OnPause ();
		}

		protected override void OnResume ()
		{
			try
			{
				//if (socket != null)
					//socket.Connect();
			}
			catch(Exception ex) 
			{
				Button btn = FindViewById<Button> (Resource.Id.btnConnect);
				btn.Text = "Connect";
			}
			base.OnResume ();
		}

		private void BtnConnectClick(object sender, EventArgs args)
		{
			Button senderButton = (Button)sender;

			if (senderButton.Text == "Disconnect") 
			{
				socket.Close ();
				senderButton.Text = "Connect";
				return;
			}

			if (adapter == null)
				adapter = BluetoothAdapter.DefaultAdapter;

			if (!adapter.IsEnabled)
				adapter.Enable ();

			if (device == null) 
			{
				var devices = adapter.BondedDevices;
				foreach (BluetoothDevice d in devices) 
				{
					if (d.Name != "HC-06")
						continue;

					device = d;
				}
			}

			//Now create the socket
			if (socket == null) {
				Java.Util.UUID uuid = Java.Util.UUID.FromString ("00001101-0000-1000-8000-00805F9B34FB");
				socket = device.CreateRfcommSocketToServiceRecord (uuid);
			}

			try
			{			
				Java.Util.UUID uuid = Java.Util.UUID.FromString ("00001101-0000-1000-8000-00805F9B34FB");
				socket = device.CreateRfcommSocketToServiceRecord (uuid);

				//Now lets connect the socket
				socket.Connect ();

				senderButton.Text = "Disconnect";
			}
			catch(Exception ex) 
			{
				throw ex;
			}

		}

		private void HandleClick(object sender, EventArgs args)
		{
			if (socket == null) 
			{
				return;
			}

			Button senderButton = (Button)sender;

			switch (senderButton.Text) 
			{
			case "1":
				SendCommand ((byte)0x01);
				break;
			case"2":
				SendCommand ((byte)0x02);
				break;
			case"3":
					SendCommand ((byte)0x03);
				break;
			case"4":
					SendCommand ((byte)0x04);
				break;
			case"5":
					SendCommand ((byte)0x05);
				break;
			case"6":
					SendCommand ((byte)0x06);
				break;
			case"7":
					SendCommand ((byte)0x07);
				break;
			case"8":
					SendCommand ((byte)0x08);
				break;
			case"9":
					SendCommand ((byte)0x09);
				break;
			case"0":
					SendCommand ((byte)0x010);
				break;
			case"Direct":
					SendCommand ((byte)0x11);
				break;
			case"Text":
					SendCommand ((byte)0x12);
				break;
			case"Left":
					SendCommand ((byte)0x15);
				break;
			case"Up":
					SendCommand ((byte)0x16);
				break;
			case"Menu":
					SendCommand ((byte)0x19);
				break;
			case"Clock #":
					SendCommand ((byte)0x20);
				break;
			case"Scan":
					SendCommand ((byte)0x13);
				break;
			case"Cat/Fold":
					SendCommand ((byte)0x14);
				break;
			case"Right":
					SendCommand ((byte)0x17);
				break;
			case"Down":
					SendCommand ((byte)0x18);
				break;
			case"Sound":
					SendCommand ((byte)0x21);
				break;
			case"Phone":
					SendCommand ((byte)0x22);
				break;
			case"Radio Power":
					SendCommand ((byte)0x27);
				break;
			case"Vol +":
					SendCommand ((byte)0x25);
				break;
			case"AM/FM":
					SendCommand ((byte)0x28);
				break;
			case"CD":
					SendCommand ((byte)0x30);
				break;
			case"Tune +":
					SendCommand ((byte)0x32);
				break;
			case"Play Pause Ok":
					SendCommand ((byte)0x34);
				break;
			case"Vol -":
					SendCommand ((byte)0x26);
				break;
			case"Sirius":
					SendCommand ((byte)0x29);
				break;
			case"Aux":
					SendCommand ((byte)0x31);
				break;
			case"Tune - ":
					SendCommand ((byte)0x33);
				break;

			}
		}

		private void SendCommand(byte command)
		{
			byte[] dataToSend = new[] { (byte)command, (byte)(0x013) }; //0x13 is CR
			socket.OutputStream.Write (dataToSend, 0, dataToSend.Length);


			//TextView txtView = FindViewById<TextView> (Resource.Id.txtStatus);
			//txtView.Text = command.ToString ("X");
		}
	}
}


